const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    itemDetail: {
      sender: {
        senderCompanyName: '',
        senderPhone: '',
        senderProvince: '',
        senderCity: '',
        senderDistict: '',
        senderTown: ''
      },
      consignee: {
        consigneeCompanyName: '',
        consigneePhone: '',
        consigneeProvince: '',
        consigneeCity: '',
        consigneeDistict: '',
        consigneeTown: ''
      }
    },
    serviceModeList: ['请选择服务方式', '门到门', '门到港', '港到港', '港到门'],
    services: ['请选择支付方式', '寄方现结', '寄方月结', '收放现金', '收方月结'],
    // serviceModeObj: [{
    //     name: '请选择服务方式',
    //     pickUpTime: '期望提货时间',
    //     deliveryTime: '期望送货时间'
    //   },
    //   {
    //     name: '上门提货-送货上门',
    //     pickUpTime: '期望提货时间',
    //     deliveryTime: '期望送货时间'
    //   },
    //   {
    //     name: '上门提货-站点自提',
    //     pickUpTime: '期望提货时间',
    //     deliveryTime: '计划提货时间'
    //   },
    //   {
    //     name: '自送站点-送货上门',
    //     pickUpTime: '计划送达时间',
    //     deliveryTime: '期望送货时间'
    //   },
    //   {
    //     name: '自送站点-站点自提',
    //     pickUpTime: '计划送达时间',
    //     deliveryTime: '计划提货时间'
    //   }
    // ],
    serviceMode: '请选择服务方式',
    serviceType:0,
    service:'请选择支付方式',
    payType:0,
    pickUpTime: '取件日期',
    deliveryTime: '投递日期',
    serviceModeIndex: 0,
    goodsTotal: '',
    shippingType: 1,
    serviceList: [{
        id: 1,
        name: '签单返还',
        value: '0',
        checked: false
      },
      {
        id: 2,
        name: '送旧取新',
        value: '1',
        checked: false
      },
      {
        id: 3,
        name: '包装',
        value: '2',
        checked: false
      }, {
        id: 4,
        name: '返件',
        value: '3',
        checked: false
      }
    ],
    carrier: {
      name: '宁波华升物流',
      tag: ['运费 500/立方', '提货 10/公里', '送货 10/公里', '包装50/立方'],
      evaluate: 4.7
    },
    serviceAgreementChecked: true,
    num:0,//货物件数
    lf:0,//立方
    qk:0,//千克
    headinp:'',//客户单号
    bxtxt:0,
    bxftxt: 0,
    zdtxt: 0,
    bztxt: 0,
    othertxt: 0,
    orderGoodInfosList:[],
    he:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow(options) {
      var that =this
    console.log(app.globalData.items)

      var item = app.globalData.items
      
      console.log(item,"898989")
      var lf = 0, qk = 0, num = 0
      for (var i = 0; i < item.length; i++) {
        num += Number(item[i].nums)
        lf += Number(item[i].volume)
        qk += Number(item[i].realWeight)
      }
      that.setData({
        num: num,//货物合计
        lf: lf,
        qk: qk,
        orderGoodInfosList: item
      })

   
    this.setServiceMode(this.data.serviceModeIndex);
  },
  onLoad(options){
    var that=this
    var company = wx.getStorageSync("COMPANY")
    var tokenL = app.globalData.token
    console.log(company)


    wx.request({
      url: app.globalData.apiServer + "/api/ValueAddedService/All",
      data: {
        id: company.id,
        companyName: company.companyName
      },
      method: "POST",
      header: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + tokenL,
        "LoginType": 4
      },
      success: function (res) {
        if (res.data.responseCode == 1000) {
          var serviceList=[]
          for(var i=0;i<res.data.obj.length;i++){
            serviceList.push(res.data.obj[i])
            serviceList[serviceList.length - 1]['checked'] = false
          }
          that.setData({
            serviceList: serviceList
          })
        } else {
          wx.showToast({
            title:'请求失败',
            icon: 'loading',
            duration: 2000
          });
        }
      }
    })


  },
  headinp(e){//客户单号
    var that= this
      that.setData({
        headinp: e.detail.value
      })
  },
  bxtxt(e){//保险费率
    var that = this

    that.setData({
      bxtxt: e.detail.value
    })
    var he = Number(that.data.bxtxt) + Number(that.data.bxftxt) + Number(that.data.zdtxt) + Number(that.data.bztxt) + Number(that.data.othertxt)
    that.setData({
      he:he
    })
  },
  bxftxt(e) {//保险费
    var that = this
    that.setData({
      bxftxt: e.detail.value
    })
    var he = Number(that.data.bxtxt) + Number(that.data.bxftxt) + Number(that.data.zdtxt) + Number(that.data.bztxt) + Number(that.data.othertxt)
    that.setData({
      he: he
    })
   },
  zdtxt(e) {//制单费
    var that = this
    that.setData({
      zdtxt: e.detail.value
    })
    var he = Number(that.data.bxtxt) + Number(that.data.bxftxt) + Number(that.data.zdtxt) + Number(that.data.bztxt) + Number(that.data.othertxt)
    that.setData({
      he: he
    })
   },
  bztxt(e) {//包装费
    var that = this
    that.setData({
      bztxt: e.detail.value
    })
    var he = Number(that.data.bxtxt) + Number(that.data.bxftxt) + Number(that.data.zdtxt) + Number(that.data.bztxt) + Number(that.data.othertxt)
    that.setData({
      he: he
    })
   },
  othertxt(e) {//其他费
    var that = this
    that.setData({
      othertxt: e.detail.value
    })
    var he = Number(that.data.bxtxt) + Number(that.data.bxftxt) + Number(that.data.zdtxt) + Number(that.data.bztxt) + Number(that.data.othertxt)
    that.setData({
      he: he
    })
   },
  // 下单
  placeOrder() {
    var that = this
    var company = wx.getStorageSync("COMPANY")
    var tokenL = app.globalData.token
    var valueAddedServiceList=[]//增值业务
    for (var i = 0; i < that.data.serviceList.length;i++){
      if (that.data.serviceList[i].checked == true){
        valueAddedServiceList.push({id:that.data.serviceList[i].id})
       }
    }
    // if (valueAddedServiceList.length == 0){
    //   wx.showToast({
    //     title: '请选择增值业务',
    //     icon: 'loading',
    //     duration: 2000
    //   });
    //   return false     
    // }
    // if(that.data.headinp == ''){
    //   wx.showToast({
    //     title: '请输入单号',
    //     icon: 'loading',
    //     duration: 2000
    //   });
    //   return false
    // }
    
    if (that.data.itemDetail.sender.senderCompanyName == '' || that.data.itemDetail.consignee.consigneeCompanyName == ''  ) {
      wx.showToast({
        title: '请输入地址',
        icon: 'loading',
        duration: 2000
      });
      return false
    }
    if (that.data.serviceMode == '请选择服务方式') {
      wx.showToast({
        title: '选择服务方式',
        icon: 'loading',
        duration: 2000
      });
      return false
    }
    if (that.data.service == '请选择支付方式') {
      wx.showToast({
        title: '选择支付方式',
        icon: 'loading',
        duration: 2000
      });
      return false
    }
    if (that.data.pickUpTime == '取件日期') {
      that.setData({
        pickUpTime:''
      })
    }
    if (that.data.deliveryTime == '投递日期') {
      that.setData({
        deliveryTime: ''
      })
    }
    if (that.data.num == 0) {
      wx.showToast({
        title: '请填写货物信息',
        icon: 'loading',
        duration: 2000
      });
      return false
    }
    wx.request({
      url: app.globalData.apiServer + "/api/Orders/AddForWeChatTemp",
      data: {
        company: {
          id: company.id,
          companyName: company.companyName
        },
        customerOrderNumber: that.data.headinp, //客户订单编号
        senderName: that.data.itemDetail.sender.senderCompanyName,
        senderProvince: that.data.itemDetail.sender.senderProvince,
        senderCity:that.data.itemDetail.sender.senderCity ,
        senderDistict:that.data.itemDetail.sender.senderDistict ,
        senderTown:that.data.itemDetail.sender.senderTown,
        senderPhone: that.data.itemDetail.sender.senderPhone, 

        consigneeName: that.data.itemDetail.consignee.consigneeCompanyName,
        consigneeProvince: that.data.itemDetail.consignee.consigneeProvince, 
        consigneeCity: that.data.itemDetail.consignee.consigneeCity,
        consigneeDistict: that.data.itemDetail.consignee.consigneeDistict,
        consigneeTown: that.data.itemDetail.consignee.consigneeTown,
        consigneePhone: that.data.itemDetail.consignee.consigneePhone,
        serviceType: that.data.serviceType,//服务方式
        payType: that.data.payType,//支付方式
        orderGoodInfosList: that.data.orderGoodInfosList,//货物列表
        insuranceRate: that.data.bxtxt,//保险费率
        insuredCost: that.data.bxftxt,//保价费用
        otherFee: that.data.othertxt,//其他费用
        formFee: that.data.zdtxt,//制单费
        packagingFee: that.data.bztxt,//包装费
        beginTime: that.data.pickUpTime,//提货日期
        endTime: that.data.deliveryTime,//送货日期
        shippingType: that.data.shippingType,//运输方式
        totalFee:that.data.he,//合计费用
        valueAddedServiceList: valueAddedServiceList//增值业务
      },
      method: "POST",
      header: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + tokenL,
        "LoginType": 4
      },
      success: function (res) {
        if (res.data.responseCode == 1000) {
          wx.showToast({
            title: '建单成功',
            icon: 'loading',
            duration: 2000
          });
          app.globalData.items=[]
          setTimeout(function () {
            that.toOrder()
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'loading',
            duration: 2000
          });
        }

      }

    })
    
  },

  // 选择服务模式
  bindPickerChange: function (e) {
    this.setData({
      serviceModeIndex: e.detail.value
    })
    this.setServiceMode(e.detail.value);
  },

  setServiceMode(idx) {//服务方式

    console.log(idx,"--")
    let that = this;
    that.setData({
      serviceMode: that.data.serviceModeList[idx],
      serviceType: idx
      
      // serviceMode: that.data.serviceModeObj[idx].name,
      // pickUpTime: that.data.serviceModeObj[idx].pickUpTime,
      // deliveryTime: that.data.serviceModeObj[idx].deliveryTime,
    })
  },
  bindPicker(e){//支付方式
    console.log('支付方式', e.detail.value)
    let that = this;
    that.setData({
      service: that.data.services[e.detail.value],
      payType: e.detail.value
      // serviceMode: that.data.serviceModeObj[idx].name,
      // pickUpTime: that.data.serviceModeObj[idx].pickUpTime,
      // deliveryTime: that.data.serviceModeObj[idx].deliveryTime,
    })   
  }
  ,

  // 设置提货时间
  bindPickupTimeChange(e) {
    console.log(e)
    this.setData({
      pickUpTime: e.detail.value
    })
  },

  // 设置送货时间
  bindDeliveryTimeChange(e) {
    this.setData({
      deliveryTime: e.detail.value
    })
  },

  // 选择地址
  chooseAddress(e) {
    let _type = e.currentTarget.dataset.type,
      that = this;
    wx.chooseAddress({
      success: function (res) {
        if (_type === 'sender') {
          that.setData({
            'itemDetail.sender.senderCompanyName': res.userName,
            'itemDetail.sender.senderPhone': res.telNumber,
            'itemDetail.sender.senderProvince': res.provinceName,
            'itemDetail.sender.senderCity': res.cityName,
            'itemDetail.sender.senderDistict': res.countyName,
            'itemDetail.sender.senderTown': res.detailInfo
          })
        } else {
          that.setData({
            'itemDetail.consignee.consigneeCompanyName': res.userName,
            'itemDetail.consignee.consigneePhone': res.telNumber,
            'itemDetail.consignee.consigneeProvince': res.provinceName,
            'itemDetail.consignee.consigneeCity': res.cityName,
            'itemDetail.consignee.consigneeDistict': res.countyName,
            'itemDetail.consignee.consigneeTown': res.detailInfo
          })
        }
      }
    })
  },

  // 选择运输方式
  chooseShipping(e) {
    let _type = e.currentTarget.dataset.type,
      that = this;
    console.log(_type);
    that.setData({
      shippingType: _type
    })
    if (_type == 2){
      
    }
  },

  // 增值业务选择
  serviceValChange: function (e) {
    console.log(e)
    let serviceList = this.data.serviceList;
    let checkArr = e.detail.value;
    for (let i = 0; i < serviceList.length; i++) {
      if (checkArr.indexOf(serviceList[i].id) != -1) {
      //if (checkArr.indexOf(i + "") != -1) {
        serviceList[i].checked = true;
      } else {
        serviceList[i].checked = false;
      }
    }
    this.setData({
      serviceList: serviceList
    })
  },


  // 服务协议选择
  checkboxChange(e) {
    this.setData({
      serviceAgreementChecked: e.detail.value.length > 0 ? true : false
    })
  },

  // 设置货物合计
  setGoodsTotal() {
    wx.navigateTo({
      url: '../goodsInfo/goodsInfo',
    })
  },


  // 承运商
  carrierChange() {
    wx.navigateTo({
      url: '../carrier/carrier',
    })
  },
   toOrder(){//跳转接单
      wx.redirectTo({
        url: '../order/order',
      })
   },

})